//
//  NSString+Hash.m
//
//  Created by Tom Corwine on 5/30/12.
//

#import "NSString+Hash.h"
#import "LKServerAPI.h"
#import "NSString+Regex.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
#import "LKUserInforTool.h"
@implementation NSString (Hash)

- (NSString *)md5String
{
	const char *string = self.UTF8String;
	int length = (int)strlen(string);
	unsigned char bytes[CC_MD5_DIGEST_LENGTH];
	CC_MD5(string, length, bytes);
	return [self stringFromBytes:bytes length:CC_MD5_DIGEST_LENGTH];
}




#pragma mark - Helpers

- (NSString *)stringFromBytes:(unsigned char *)bytes length:(int)length
{
	NSMutableString *mutableString = @"".mutableCopy;
	for (int i = 0; i < length; i++)
		[mutableString appendFormat:@"%02x", bytes[i]];
	return [NSString stringWithString:mutableString];
}



/// 设置无type的sign生成方式
/// @param uid uid
+(NSString *)encodeWithNoType:(NSString *)uid{
    NSString *returnStr = @"";
    returnStr = [NSString stringWithFormat:@"%@jmt_remark=%@",returnStr,@""];
    returnStr = [NSString stringWithFormat:@"%@&timestamp=%@",returnStr,@"1593763932.0041919"];
        returnStr = [NSString stringWithFormat:@"%@&token=%@",returnStr,@"54474D396310AE005962A9C0C99081F4505AFEC6"];
    returnStr = [NSString stringWithFormat:@"%@&total_amount=%@",returnStr,@"50"];
    returnStr = [NSString stringWithFormat:@"%@&uid=%@",returnStr,@"27"];
    returnStr = [NSString stringWithFormat:@"%@&key=%@",returnStr,Signature];
    returnStr = [self MD5ForUpper32Bate:returnStr];
    return  returnStr;
}



/// 根据type生成签名signature
/// @param type type
+(NSString *)encodeWithType:(NSString *)type{
    NSString *returnStr = @"";
    returnStr = [NSString stringWithFormat:@"timestamp=%@",@([NSString getNowTimestamp])];
    returnStr = [NSString stringWithFormat:@"%@&token=%@",returnStr,[LKUserInforTool account].token];
    returnStr = [NSString stringWithFormat:@"%@&type=%@",returnStr,type];
    returnStr = [NSString stringWithFormat:@"%@&key=%@",returnStr,Signature];

    returnStr = [self MD5ForUpper32Bate:returnStr];
    return returnStr;
}

/** *  加密url

       dataDic  要进行签名的字典

       character  随机16位数

*/

+(NSString*)encoingWithDic:(NSMutableDictionary*)dataDic
    {
           //当前时间戳
        [dataDic setObject:@([NSString getNowTimestamp]) forKey:@"timestamp"];
        [dataDic setObject:[LKUserInforTool account].token forKey:@"token"];
        
        NSArray *keys = [dataDic allKeys];
            //按字母顺序排序

            NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)

            {
                return [obj1 compare:obj2 options:NSNumericSearch];

             }];

            NSString *returnStr = @"";

            //拼接字符串

            for (int i=0;i<sortedArray.count;i++){

            NSString *category = sortedArray[i];

            if (i==0) {

            returnStr =[NSString stringWithFormat:@"%@=%@",category,dataDic[category]];
                
            }else{

            returnStr = [NSString stringWithFormat:@"%@&%@=%@",returnStr,category,dataDic[category]];}
              }
            /*拼接上key得到stringSignTemp*/
//
          returnStr = [NSString stringWithFormat:@"%@&key=%@",returnStr,Signature];

            /*md5加密*/
            returnStr = [self MD5ForUpper32Bate:returnStr];
            return returnStr;
                
    }

            //md5 32位加密 （大写）

+(NSString *)MD5ForUpper32Bate:(NSString *)str{
    
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02X", result[i]];
    }

    return digest;
}

            //md5 32位 加密 （小写）

+ (NSString *)md5:(NSString *)str {

            const char *cStr = [str UTF8String];

            unsigned char result[32];

            CC_MD5( cStr, strlen(cStr), result );

            return [NSString stringWithFormat:

            @"xxxxxxxxxxxxxxxx",

            result[0],result[1],result[2],result[3],

            result[4],result[5],result[6],result[7],

            result[8],result[9],result[10],result[11],

            result[12],result[13],result[14],result[15],

            result[16], result[17],result[18], result[19],

            result[20], result[21],result[22], result[23],

            result[24], result[25],result[26], result[27],

            result[28], result[29],result[30], result[31]];

            }

            //产生16位随机数

        + (NSString *)generateFradomCharacter

        {

        static int kNumber = 16;

        NSString *sourceStr = @"abcdefghijklmnopqrstuvwxyz0123456789";

        NSMutableString *resultStr = [[NSMutableString alloc] init];

        for (int i = 0; i < kNumber; i++)

        {

        unsigned index =  arc4random() % [sourceStr length];

        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];

        [resultStr appendString:oneStr];

        }

        return resultStr;

}


@end
